package com.timing.feedback.controller;


import com.timing.feedback.entity.Opinions;
import com.timing.feedback.service.OpinionsService;
import com.timing.feedback.vo.ResultVO;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cwh
 * @since 2021-05-27
 */
@RestController
@RequestMapping("/opinion")
public class OpinionsController {
    @Resource
    private OpinionsService opinionsService;

    @PostMapping
    public ResultVO saveOpinions(@RequestBody @Validated Opinions opinions, BindingResult bindingResult){

        // 参数校验
        if (bindingResult.hasErrors()) {
            String messages = bindingResult.getAllErrors()
                .stream()
                .map(ObjectError::getDefaultMessage)
                .reduce((m1, m2) -> m1 + "；" + m2)
                .orElse("参数输入有误！");
            throw new IllegalArgumentException(messages);
        }

        return  opinionsService.saveOpinionsAndCount(opinions);
    }
}

